﻿using BeetleX.Buffers;
using BeetleX.XRPC;
using BeetleX.XRPC.Clients;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Chat
{
    public class Client
    {
        static Client()
        {
            CurrentUser = new ChatUser();
        }
        public static ChatUser CurrentUser { get; }

        public static IChatService CurrentService { get; set; }

        public static async Task<IChatService> StartClient()
        {
            return await Task.Run<IChatService>(() =>
            {
                XRPCClient client = new XRPCClient("localhost", 9999, 2);
                client.Options.LogToFile = false;
                //client.Options.ParameterFormater = new MyParameterFormater();
                client.NetError = new BeetleX.Clients.EventClientError((client, e) =>
                {
                    if (e.Error != null)
                        Console.WriteLine(e.Error.Message);
                    if (!string.IsNullOrEmpty(e.Message))
                        Console.WriteLine(e.Message);
                });
                client.Connect();
                Console.WriteLine("客户端启动成功！");
                return client.Create<IChatService>(CurrentUser.Id);
            });
        }
        /// <summary>
        /// 改写参数格式化
        /// 比较难实现
        /// </summary>
        /// <remarks>
        /// header 4
        /// 
        /// </remarks>
        class MyParameterFormater : BeetleX.XRPC.Packets.IParameterFormater
        {
            public static int ToInt(ArraySegment<byte> data)
            {
                uint i = 0;
                i ^= data[0];
                i <<= 8;
                i ^= data[1];
                i <<= 8;
                i ^= data[2];
                i <<= 8;
                i ^= data[3];
                return (int)i;
            }

            object BeetleX.XRPC.Packets.IParameterFormater.Decode(Options rpcOption, Type type, ArraySegment<byte> data)
            {
                int length = data.Count;
                if (length == 0)
                {
                    if (type.IsValueType)
                    {
                        return Activator.CreateInstance(type);
                    }
                    return null;
                }
                else
                {
                    if(type==null)
                    {
                        var result = new byte[length];
                        System.Buffer.BlockCopy(data.Array, data.Offset, result, 0, data.Count);
                        return result;
                    }
                    else
                    {
                        int len = ToInt(data.Slice(0, 4));
                        data.Slice(4, len);
                    }
                }
                throw new NotImplementedException();
            }

            void BeetleX.XRPC.Packets.IParameterFormater.Encode(Options rpcOption, object data, PipeStream stream)
            {
                throw new NotImplementedException();
            }
        }
    }
}
